package com.itheima.reggie.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.Mapper.OrederMapper;
import com.itheima.reggie.Service.AddressService;
import com.itheima.reggie.Service.OrderDetailService;
import com.itheima.reggie.Service.OrderService;
import com.itheima.reggie.Service.ShoppingCartService;
import com.itheima.reggie.exception.BaseContext;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.pojo.AddressBook;
import com.itheima.reggie.pojo.OrderDetail;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.pojo.ShoppingCart;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class OrderServiceImpl extends ServiceImpl<OrederMapper, Orders> implements OrderService {

    @Autowired
    private AddressService addressService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrderService orderService;
    @Override
    @Transactional
    public void submit(Orders orders ) {

        Long userid = BaseContext.getCurrentId();
        AtomicInteger amount = new AtomicInteger(0);//在多线程计算总金额
        long orderId = IdWorker.getId();//订单号
        //查询地址数据
        System.out.println("11111");
        Long addressId=orders.getAddressBookId();
       AddressBook addressBook= addressService.getById(addressId);
       if(addressBook==null)
       {
           throw new CustomException("地址有误请重新填地址");
       }
       //订单表
        orders.setUser_id(userid);
        orders.setNumber(String.valueOf(orderId));
        orders.setStatus(2);
        orders.setAddressBookId(orders.getAddressBookId());
        orders.setOrder_time(LocalDateTime.now());
        orders.setPayMethod(1);
        orders.setPhone(addressBook.getPhone());
        orders.setUserName(addressBook.getConsignee());
        orders.setAmount(new BigDecimal(amount.get()));//总金额
        orders.setCheckoutTime(LocalDateTime.now());
        orders.getAddress((addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName())
                + (addressBook.getCityName() == null ? "" : addressBook.getCityName())
                + (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())
                + (addressBook.getDetail() == null ? "" : addressBook.getDetail()));

        QueryWrapper<ShoppingCart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userid);
        List<ShoppingCart> shoppingCarts =shoppingCartService.list(queryWrapper);
        //订单明细表
        OrderDetail orderDetail = new OrderDetail();
        for(ShoppingCart shopping:shoppingCarts)
        {

            orderDetail.setOrder_id(orderId);
            orderDetail.setName(shopping.getName());
            orderDetail.setSetmeal_id(shopping.getSetmealId());
            orderDetail.setDish_flavor(shopping.getDishFlavor());
           orderDetail.setDish_id(shopping.getDishId());
           orderDetail.setNumber(shopping.getNumber());
            amount.addAndGet(shopping.getAmount().multiply(new BigDecimal(shopping.getNumber())).intValue());
            orderDetail.setAmount(shopping.getAmount());
        }
        orderDetailService.save(orderDetail);
        orderService.save(orders);
    }

}
